package com.example.myapplication.storage;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.myapplication.R;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

public class ExternalStorageActivity extends AppCompatActivity {
    Button btn1,btn2;
    EditText editName,editPSW;
    TextView textView;
    String fileName = "hello_file.txt";
    ///可用
    boolean mExternalStorageAvailable = false;
    ///可写
    boolean mExternalStorageWriteable = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_external_storage);
        btn1 = findViewById(R.id.btn1);
        btn2 = findViewById(R.id.btn2);
        editName = findViewById(R.id.editName);
        editPSW = findViewById(R.id.editPWD);
        textView =  findViewById(R.id.text);
    }

    public void btn1Click(View view) throws IOException {
        String name = editName.getText().toString();
        String pwd = editPSW.getText().toString();
        ///判断外部存储设备的状态
        checkExternalStorage();
        if(mExternalStorageWriteable){
            OutputStream out = new FileOutputStream(new File(getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS),fileName));
            out.write((name+"##"+pwd).getBytes());
            if(out!=null){
                out.close();
            }
        }
        Toast.makeText(this,"登陆成功",Toast.LENGTH_LONG).show();
    }



    public void btn2Click(View view) throws IOException {
        ///判断外部存储设备的状态
        checkExternalStorage();
        if(mExternalStorageAvailable){
            File file = new File(getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS),fileName);
            if(file.exists()){
                FileInputStream in = new FileInputStream(file);
                byte[] input = new byte[in.available()];
                while (in.read(input) != -1){
                    String str = new String(input);
                    String [] s= str.split("##");
                    textView.setText("用户名："+s[0]+"  密码"+s[1]);
                }
                if(in!=null){
                    in.close();
                }
            }
        }
        Toast.makeText(this,"登陆成功",Toast.LENGTH_LONG).show();
    }
    ///判断外部存储设备的状态
    private void checkExternalStorage() {
        String state  = Environment.getExternalStorageState();
        ///可用/可写
        if(Environment.MEDIA_MOUNTED.equals(state)){
             mExternalStorageAvailable = true;
             mExternalStorageWriteable = true;
        }else if(Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)){
             mExternalStorageAvailable = true;
             mExternalStorageWriteable = false;
        }else{
             mExternalStorageAvailable = mExternalStorageWriteable = false;
        }

    }
}